using System;
using System.Collections;
using System.Xml.Serialization;
/*
  <section type="text"> <!-- one of the three predefined sections that differ by importance -->
			<term-frequency>3</term-frequency> <!--term frequency in this section -->
			<sentence>
				<number>1</number> <!-- in the sentence number 1 this term comes at third place -->
				<place>3</place>
				<next-id/> <!-- this is a last term in the sentence -->
			</sentence>
			<sentence>
				<number>2</number> <!-- in the sentence number 2 this term comes at first place -->
				<place>1</place>
				<connecting-word/>
				<next-id>3</next-id>  <!-- the next term in this sentence is 3 -->
			</sentence>
			<sentence>
				<number>4</number>
				<place>6</place>
				<connecting-word>on</connecting-word>
				<next-id>2</next-id>
			</sentence>
	</section>
*/

namespace Slaks.DocumentSerialization.Document
{
	public class Section
	{
		public enum SectionType
		{
			[XmlEnum("link")] Link,
			[XmlEnum("text")] Text,
			[XmlEnum("title")] Title,
			[XmlEnum("meta-keyword")] MetaKeyword,
			[XmlEnum("meta-description")] MetaDescription,
			[XmlEnum("heading")] Heading,
			[XmlEnum("caption")] Caption
		} ;

		private SectionType m_sectionType;
		private long m_termFrequency;
		private ArrayList m_sentences;

		public Section()
		{
			m_sentences = new ArrayList();
		}

		[XmlAttribute("type")]
		public SectionType Type
		{
			get { return m_sectionType; }
			set { m_sectionType = value; }
		}

		public bool SetSectionType(string sectionType)
		{
			if (string.Equals(Grammar.SectionType.CaptionTypeAttribute, sectionType)) Type = SectionType.Caption;
			else if (string.Equals(Grammar.SectionType.MetaDescriptionTypeAttribute, sectionType)) Type = SectionType.MetaDescription;
			else if (string.Equals(Grammar.SectionType.HeadingTypeAttribute, sectionType)) Type = SectionType.Heading;
			else if (string.Equals(Grammar.SectionType.LinkTypeAttribute, sectionType)) Type = SectionType.Link;
			else if (string.Equals(Grammar.SectionType.MataKeywordTypeAttribute, sectionType)) Type = SectionType.MetaKeyword;
			else if (string.Equals(Grammar.SectionType.TextTypeAttribute, sectionType)) Type = SectionType.Text;
			else if (string.Equals(Grammar.SectionType.TitleTypeAttribute, sectionType)) Type = SectionType.Title;
			else return false;

			return true;
		}

		public string GetSectionTypeString()
		{
			if (Type == SectionType.Caption) return Grammar.SectionType.CaptionTypeAttribute;
			if (Type == SectionType.MetaDescription) return Grammar.SectionType.MetaDescriptionTypeAttribute;
			if (Type == SectionType.Heading) return Grammar.SectionType.HeadingTypeAttribute;
			if (Type == SectionType.Link) return Grammar.SectionType.LinkTypeAttribute;
			if (Type == SectionType.MetaKeyword) return Grammar.SectionType.MataKeywordTypeAttribute;
			if (Type == SectionType.Text) return Grammar.SectionType.TextTypeAttribute;
			return Grammar.SectionType.TitleTypeAttribute;
		}

		public static string GetSectionStringByType(SectionType type)
		{
			string section;
			switch(type)
			{
				case SectionType.Caption : section = Grammar.SectionType.CaptionTypeAttribute;
				 break;

				case SectionType.MetaDescription : section = Grammar.SectionType.MetaDescriptionTypeAttribute;
					break;

				case SectionType.Heading : section = Grammar.SectionType.HeadingTypeAttribute;
					break;

				case SectionType.Link : section = Grammar.SectionType.LinkTypeAttribute;
					break;

				case SectionType.MetaKeyword : section = Grammar.SectionType.MataKeywordTypeAttribute;
					break;

				case SectionType.Text : section = Grammar.SectionType.TextTypeAttribute;
					break;

				case SectionType.Title : section = Grammar.SectionType.TitleTypeAttribute;
					break;

				default:
					throw new Exception("Section type not known");

			}
			return section;
		}

		public static SectionType GetSectionTypeByString(string sectionTypeString)
		{
			if (sectionTypeString == Grammar.SectionType.CaptionTypeAttribute) return SectionType.Caption;
			if (sectionTypeString == Grammar.SectionType.MetaDescriptionTypeAttribute) return SectionType.MetaDescription;
			if (sectionTypeString == Grammar.SectionType.HeadingTypeAttribute) return SectionType.Heading;
			if (sectionTypeString == Grammar.SectionType.LinkTypeAttribute) return SectionType.Link;
			if (sectionTypeString == Grammar.SectionType.MataKeywordTypeAttribute) return SectionType.MetaKeyword;
			if (sectionTypeString == Grammar.SectionType.TextTypeAttribute) return SectionType.Text;
			if (sectionTypeString == Grammar.SectionType.TitleTypeAttribute) return SectionType.Title;
			throw new Exception("No such section type" + sectionTypeString);
		}


		[XmlElement("term-frequency")]
		public long TermFrequency
		{
			get
			{
				m_termFrequency = m_sentences.Count;
				return m_termFrequency;
			}
			set { m_termFrequency = value; }
		}

		[XmlArrayItem("sentence", typeof (Sentence), IsNullable = false)]
		[XmlArray]
		public IList Sentences
		{
			get { return m_sentences; }
		}

		/// <summary>
		/// Get the sentence with specified number and place
		/// </summary>
		/// <param name="number">sentence number for given term</param>
		/// <param name="place">the term place in the sentence</param>
		/// <returns></returns>
		public Sentence GetSentence(int number, int place)
		{
			foreach (Sentence sentence in m_sentences)
			{
				if (sentence.Place == place && sentence.Number == number) return sentence;
			}

			return null;
		}

		/// <summary>
		/// Create new instance of the Section class
		/// </summary>
		/// <returns></returns>
		public static Section CreateNewSection()
		{
			return new Section();
		}
	}
}